import {
  reqGoodList
} from '../../api/index';
Page({
  data: {
    goodList: [], //存储商品分页数据
    page: 1,
    limit: 10,
    status: 'more',
    category2Id:''
  },
  onLoad(options) {
    options.category2Id&&this.setData({category2Id:options.category2Id})
    this.getGoods();
  },
  //获取商品分页数据
  async getGoods() {
    //获取参数
    let {
      page,
      limit,
      goodList,
      category2Id
    } = this.data;
    //发请求之前正在加载
    this.setData({
      status: 'loading'
    })
    try {
      let result = await reqGoodList({
        page,
        limit,
        category2Id
      });
      if (result.data.records.length > 0) {
        if (result.data.records.length < 10) {
          this.setData({
            status: 'nomore'
          })
        } else {
          this.setData({
            status: 'more'
          })
        }
        let list = result.data.records;
        goodList.push(...list)
        console.log(goodList);
        this.setData({
          goodList
        })
      } else {
        this.setData({
          status: 'nomore'
        })
      }

    } catch (error) {
      this.setData({
        status: 'error'
      })
    }
  },
  onReachBottom() {
    if (this.data.status == 'more') {
      this.setData({
        page: this.data.page + 1
      })
      //触底再次获取分页的数据
      this.getGoods();
    }
  },
  goHome(){
    wx.reLaunch({
      url: '/pages/home/index',
    })
  }
})